LanguageExt.Core

LanguageExt.Core Monads Monadic conditionals

guard, when, and unless allow for conditional operations and short-cutting in monadic expressions.

Guards

Guards are used to stop the monadic expression continuing if a flag is true (for guard) or false (for guardnot).

They only work with monads that have an 'alternative value' (which is usually used as the error condition: Left in Either for example). An alternative value is provided when the guard triggers:

from x in ma
from _ in guard(x == 100, Error.New("x should be 100"))
select x;

Supported monads are:

Either
EitherUnsafe
EitherAsync
Fin
Validation
Aff
Eff

When and Unless

when and unless are similar to guards, but instead of providing the alternative value, you provide an alternative monad to run. This monad could be in a failed state, or it could run a successful side effect (an Aff calling Console<RT>.writeLine() for example).

from x in ma
from _ in when(x == 100, Console.writeLine<RT>("x is 100, finally!"))
select x;

Contents

class Prelude Source #

Methods

method K<F, Unit> when <F> (bool flag, K<F, Unit> alternative) Source #

where F : Applicative<F>

Conditional execution of Applicative expressions

Run the alternative when the flag is true, return pure () when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, Console.writeLine<RT>("x is 100, finally!"))
select x;

method K<F, Unit> unless <F> (bool flag, K<F, Unit> alternative) Source #

where F : Applicative<F>

Conditional execution of Applicative expressions

Run the alternative when the flag is false, return pure () when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, Console.writeLine<RT>("x should be 100!"))
select x;

method K<F, Unit> guard <F> (bool flag) Source #

where F : Alternative<F>

Guard against continuing an applicative expression

Parameters

param flag

Flag for continuing

returns

Applicative that yields () if flag is true; otherwise it yields Applicative.Empty - shortcutting the expression

method Guard<E, Unit> guard <E> (bool flag, Func<E> False) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param False

If the flag is false, this provides the error

returns

Guard

method Guard<E, Unit> guard <E> (bool flag, E False) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param False

If the flag is false, this provides the error

returns

Guard

method Guard<Error, Unit> guard (bool flag, Func<Error> False) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param False

If the flag is false, this provides the error

returns

Guard

method Guard<Error, Unit> guard (bool flag, Error False) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param False

If the flag is false, this provides the error

returns

Guard

method Guard<E, Unit> guardnot <E> (bool flag, Func<E> True) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param True

If the flag is false, this provides the error

returns

Guard

method Guard<E, Unit> guardnot <E> (bool flag, E True) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param True

If the flag is false, this provides the error

returns

Guard

method Guard<Error, Unit> guardnot (bool flag, Func<Error> True) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param True

If the flag is false, this provides the error

returns

Guard

method Guard<Error, Unit> guardnot (bool flag, Error True) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param True

If the flag is false, this provides the error

returns

Guard